﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class ArticleHotBLL
    {
        public List<ArticleHot> GetAll()
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.ArticleHots.OrderBy(p => p.Sort).ToList();
            }
        }

        public List<ArticleHot> GetAll(bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.ArticleHots.Where(p => p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
            }
        }

        public List<ArticleHot> GetByTop(int top)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.ArticleHots.Where(p => p.isEnable == true).OrderBy(p => p.Sort).Take(top).ToList();
            }
        }

        public List<ArticleHot> GetByCateId(int cateId)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.ArticleHots.Where(p => p.Category == cateId).OrderBy(p => p.Sort).ToList();
            }        
        }

        public List<ArticleHot> GetByCateId(int cateId,bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.ArticleHots.Where(p => p.Category == cateId && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
            }
        }

        public bool CheckExist(int id,int cateId)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                ArticleHot obj = data.ArticleHots.Where(p => p.Category == cateId &&  p.Id == id).FirstOrDefault();
                if (obj == null) return false;
                else return true;
            }
        }

        public int GetCountItem(int cateId)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.ArticleHots.Where(p => p.Category == cateId && p.isEnable == true).Count();
            }
        }


        //Get by id

        public ArticleHot GetById(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.ArticleHots.Where(p => p.Id == id).FirstOrDefault();
            }
        }

        public ArticleHot GetById(int id, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.ArticleHots.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
            }
        }

        //Get max Sort

        public byte GetMaxSort()
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                ArticleHot obj = data.ArticleHots.OrderByDescending(p => p.Sort).FirstOrDefault();

                if (obj == null) return 1;
                else return (byte)(obj.Sort + 1);
            }
        }

        //Swap sort

        public void SwapSort(int idFirst, int idSecond)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                ArticleHot objFirst = data.ArticleHots.Where(p => p.Id == idFirst).FirstOrDefault();
                ArticleHot objSecond = data.ArticleHots.Where(p => p.Id == idSecond).FirstOrDefault();

                int tg = objFirst.Sort;
                objFirst.Sort = objSecond.Sort;
                objSecond.Sort = tg;

                data.SubmitChanges();
            }
        }

        //Enable

        public void Enable(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                ArticleHot obj = data.ArticleHots.Where(p => p.Id == id).FirstOrDefault();
                obj.isEnable = !obj.isEnable;

                data.SubmitChanges();
            }
        }

        public void Enable(int id, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                ArticleHot obj = data.ArticleHots.Where(p => p.Id == id).FirstOrDefault();
                obj.isEnable = isEnable;

                data.SubmitChanges();
            }
        }

        //Add

        public void Add(ArticleHot obj)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                data.ArticleHots.InsertOnSubmit(obj);
                data.SubmitChanges();
            }
        }

        //Update 

        public void Update(ArticleHot obj)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                ArticleHot ArticleHot = data.ArticleHots.Where(p => p.Id == obj.Id).FirstOrDefault();
                ArticleHot.isEnable = obj.isEnable;
                ArticleHot.Sort = obj.Sort;
                data.SubmitChanges();
            }
        }

        //Delete

        public void Delete(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                ArticleHot obj = data.ArticleHots.Where(p => p.Id == id).FirstOrDefault();
                if (obj != null)
                {
                    data.ArticleHots.DeleteOnSubmit(obj);
                    data.SubmitChanges();
                }
            }
        }
    }
}
